package org.qnit

import org.apache.log4j.{Level, Logger}

import scala.collection.JavaConverters._

//相似用户搜索
object UserSimSearch {

  Logger.getLogger("org").setLevel(Level.WARN)

  val host = "node3"
  val port: Int = 19530
  val collectionName = "user_embedding"
  val collectionName1 = "user_label_w2v"
  val milvusTemplate = new MilvusTemplate(host, port)

  val userId = "071b0f847b344f8897ebe6afbb1574e7"
  val term = "西红柿"

  def toMilvusKey(key: String) = {
    val hashUserIdInt = key.hashCode
    if (hashUserIdInt >= 0) s"1$hashUserIdInt".toLong else s"2${Math.abs(hashUserIdInt)}".toLong
  }

  def main(args: Array[String]): Unit = {
    val topK = args(0).toInt
    println(s"topK=$topK")
    val termKey = toMilvusKey(term)
    val userKey = toMilvusKey(userId)
    milvusTemplate.searchIPVectorById(collectionName, userKey, topK).asScala
      .foreach(kv => println(kv._1, kv._2))
    println("------------------------------")
    milvusTemplate.searchIPVectorById(collectionName1, collectionName, termKey, topK).asScala
      .foreach(kv => println(kv._1, kv._2))
  }

}
